package com.cdtu.outtake.service.impl;

import com.cdtu.outtake.domain.Shop;
import com.cdtu.outtake.dto.ShopDto;
import com.cdtu.outtake.service.IProductService;
import com.cdtu.outtake.service.ISearchService;
import com.cdtu.outtake.service.IShopService;
import com.cdtu.outtake.service.IndexService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : shl
 * @Date 2024/3/25 23:52
 */
@Service
@RequiredArgsConstructor
public class SearchServiceImpl implements ISearchService {
    private final IndexService indexService;
    private final IShopService shopService;
    private final IProductService productService;

    @Override
    public List<ShopDto> search(Long schoolId, String keyword, Boolean dineIn, Boolean takeaway, Boolean offlinePickUp, String orderType) {
        List<Shop> shops = shopService.getShopsBySchoolIdAndConditions(schoolId, keyword, dineIn, takeaway, offlinePickUp);
        shops.addAll(productService.getShopBySchoolIdAndKeyword(schoolId, keyword).stream()
                .filter(shop -> {
                    if (Boolean.TRUE.equals(dineIn) && Boolean.FALSE.equals(shop.getDineIn())) {
                        return false;
                    }
                    if (Boolean.TRUE.equals(takeaway) && Boolean.FALSE.equals(shop.getTakeaway())) {
                        return false;
                    }
                    if (Boolean.TRUE.equals(offlinePickUp) && Boolean.FALSE.equals(shop.getOfflinePickUp())) {
                        return false;
                    }
                    return true;
                })
                .collect(Collectors.toList()));
        shops = shops.stream().distinct().collect(Collectors.toList());
        return indexService.getShopDtoList(shops, orderType);
    }
}
